#include "demosystemdbus.h"
#include <QFile>
#include <polkit-qt5-1/polkitqt1-authority.h>
#include <QProcess>
#include <QDebug>

DemoSystemDbus::DemoSystemDbus(QObject *parent) : QObject(parent)
{

}

void DemoSystemDbus::creatUser(QString usr,qint64 pid)
{
    if(!checkCreateAuthorization(pid))
    {
        qDebug()<<"鉴权失败";
        return;
    }
    QString cmd;
    if(!usr.isEmpty()) {
        cmd = QString("useradd %1").arg(usr);
        QProcess::execute(cmd);
    }
}

bool DemoSystemDbus::checkCreateAuthorization(qint64 pid)
{

    PolkitQt1::Authority::Result result;

    result = PolkitQt1::Authority::instance()->checkAuthorizationSync(
                "com.demo.qt.systemdbus.action",
                PolkitQt1::UnixProcessSubject(pid),
                PolkitQt1::Authority::AllowUserInteraction);

    if (result == PolkitQt1::Authority::Yes) {
        return true;
    } else {
        return false;
    }
}
